$(document).ready(function () {
    var ft = false;
    var mctId = sessionStorage.phone;  //用户id
    var merchantId;     //展商编号
    var merchantRemark; //展商备注
    var pageNum=1;
    var pageSize=8;
    var C1 = window.location.href.split("?")[1]; //得到id=展会编号
    var ehname = C1.split("&"); //得到编号
    var al = decodeURI(ehname);
    var b = al.split("=");
    var enam = b[2];
    var sb = b[1].split(",");
    var ehid = sb[0];//展会编号
    $("#zhm").text(enam);      //设置展商参加的展会名称
    var ehId = {
        ehId: ehid,
        pageSize: pageSize,
        pageNum: pageNum
    };
    var tol=1;//总页数
    window.load=listdata(ehId);

    //请求方法
    function listdata(str) {
        $("#plans").empty();         //清除原来的内容 id=plans标签的内容
        $.ajax({
            type: "POST",
            url: "http://www.enet720.net:8080/zwkj/merchant/findByidAll",
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            async: false,
            data: JSON.stringify(str),
            success: function (data) {
                var d = data.data;
                // 如果数据查询成功，但是数据为空的判断处理
                if (d.total > 0) {
                    tol = Math.ceil(d.total / 8);
                    if (tol < 1) {
                        tol = 1;
                    }
                    var list = d.list;
                    for (var i = 0; i < list.length; i++) {
                        $("#plans").append("        <ul class=\"merchant-to-out\">\n" +
                            "                <li>\n" +
                            "                    <div class=\"row\">\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-1\">\n" +
                            "                            <span class=\"elli\" name='merid' title='" + list[i].merchantId + "'>" + list[i].merchantId + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-3\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantName + "'>" + list[i].merchantName + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantExposition + "'>" + list[i].merchantExposition + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantEMail + "'>" + list[i].merchantEMail + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantWebsite + "'>" + list[i].merchantWebsite + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantTelephone + "'>" + list[i].merchantTelephone + "</span>\n" +
                            "                        </div>\n" +
                            "                    </div>\n" +
                            "                </li>\n" +
                            "            </ul>");
                    }
                } else {
                    $("#plans").append("抱歉，你的查找的数据不足在！嘿嘿嘿........请查看其余展会，会有意想不到的内容哦！<a href='javascript:'onclick=\"javascript:history.back(-1);\">返回上一页</a>。。。");
                    tol = 1;
                }
            },
            error: function () {
                alert("出错了,抱歉！");
            }
        });
    }
    //分页组件
    $.jqPaginator('#pagination', {
        totalPages: tol,
        visiblePages: 10,
        currentPage: 1,
        wrapper: '<ul class="pagination" style="margin: 0 0"></ul>',
        first: '<li class="first"><a href="javascript:void(0);">首页</a></li>',
        prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
        next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
        last: '<li class="last"><a href="javascript:void(0);">最后一页</a></li>',
        page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
        onPageChange: function (num) {
            pageNum = num;
            $("#plans").empty();
            ehId = {
                ehId: ehid,
                pageSize: pageSize,
                pageNum: pageNum
            };
            listdata(ehId);
        }
    });
    //点击展商在右侧展示展商详情信息
    $("#plans").on("click","li",function () {
        merchantId = $(this).find($("[name=merid]")).text();
        var adata = {
            mcId: merchantId
        };
        $.ajax({
            type: "POST",
            url: "http://www.enet720.net:8080/zwkj/merchant/findById",
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            async: false,
            data: JSON.stringify(adata),
            success: function (data) {
                var d = data.data;
                // 如果数据查询成功，但是数据为空的判断处理
                if (!(d == null)) {
                    merchantId = d.merchantId;              //展商id
                    var merchantImgAddress = d.merchantImgAddress;      //图片地址
                    var merchantNature = d.merchantNature;                  //是否特装  0表示标摊 1表示特装
                    var merchantPerson = d.merchantPerson;              //联系人
                    var merchantPhone = d.merchantPhone;                //联系电话
                    var merchantTelephone = d.merchantTelephone;           //公司电话
                    var merchantName = d.merchantName;                     //展商名称
                    var merchantExposition = d.merchantExposition;          //展位号
                    var merchantEMail = d.merchantEMail;                    //展商邮箱
                    var merchantFax = d.merchantFax;                        //展商传真
                    var merchantWebsite = d.merchantWebsite;                //展商网址
                    var merchantAddress = d.merchantAddress;                 //展商地址
                    merchantRemark = d.merchantRemark;                     //展商备注
                    var zanl = "正在努力寻找....";

                    merchantImgAddress = "../resource/img/zhantia2.jpg";
                    if (merchantName === "") {
                        merchantName = zanl;
                    }
                    if (merchantNature === "0") {
                        merchantNature = "标摊"
                    } else {
                        merchantNature = "特装"
                    }
                    if (merchantExposition === "") {
                        merchantExposition = zanl;
                    }
                    if (merchantEMail === "") {
                        merchantEMail = zanl;
                    }
                    if (merchantFax === "") {
                        merchantFax = zanl;
                    }
                    if (merchantWebsite === "") {
                        merchantWebsite = zanl;
                    }
                    if (merchantTelephone === "") {
                        merchantTelephone = zanl;
                    }
                    if (merchantPerson === "") {
                        merchantPerson = zanl;
                    }
                    if (merchantPhone === "") {
                        merchantPhone = zanl;
                    }
                    if (merchantAddress === "") {
                        merchantAddress = zanl;
                    }

                    $("#merchantImgAddress").css("background-image", "url(" + merchantImgAddress + ")");
                    $("#property").val(merchantNature);
                    $("#property").attr("title",merchantNature);
                    $("#merchantName").val(merchantName);
                    $("#merchantName").attr("title",merchantName);
                    $("#merchantExposition").val(merchantExposition);
                    $("#merchantExposition").attr("title",merchantExposition);
                    $("#merchantEMail").val(merchantEMail);
                    $("#merchantEMail").attr("title",merchantEMail);
                    $("#merchantFax").val(merchantFax);
                    $("#merchantFax").attr("title",merchantFax);
                    $("#merchantWebsite").val(merchantWebsite);
                    $("#merchantWebsite").attr("title",merchantWebsite);
                    $("#merchantTelephone").val(merchantTelephone);
                    $("#merchantTelephone").attr("title",merchantTelephone);
                    $("#merchantPerson").val(merchantPerson);
                    $("#merchantPerson").attr("title",merchantPerson);
                    $("#merchantPhone").val(merchantPhone);
                    $("#merchantPhone").attr("title",merchantPhone);
                    $("#merchantAddress").val(merchantAddress);
                    $("#merchantAddress").attr("title",merchantAddress);
                    $("#merchantRemark").val(Remark);
                    $("#merchantRemark").attr("title",Remark);
                } else {

                }
            },
            error: function () {
                alert("出错了,抱歉！")
            }
        });
    });
    //搜索展商接口
    $("#seek-bt").click(function () {
        $("#plans").empty();
        var sekinput = $("#seek-in").val();
        $.ajax({
            type: "POST",
            url: "http://www.enet720.net:8080/zwkj/merchant/findByMerchant",
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            async: false,
            data: JSON.stringify({mcName: sekinput}),
            success: function (data) {
                var d = data.data;
                // 如果数据查询成功，但是数据为空的判断处理
                if (d.total > 0) {
                    tol = Math.ceil(d.total / 8);
                    if (tol < 1) {
                        tol = 1;
                    }
                    var list = d.list;
                    for (var i = 0; i < list.length; i++) {
                        $("#plans").append("        <ul class=\"merchant-to-out\">\n" +
                            "                <li>\n" +
                            "                    <div class=\"row\">\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-1\">\n" +
                            "                            <span class=\"elli\" name='merid' title='" + list[i].merchantId + "'>" + list[i].merchantId + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-3\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantName + "'>" + list[i].merchantName + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantExposition + "'>" + list[i].merchantExposition + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantEMail + "'>" + list[i].merchantEMail + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantWebsite + "'>" + list[i].merchantWebsite + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].merchantTelephone + "'>" + list[i].merchantTelephone + "</span>\n" +
                            "                        </div>\n" +
                            "                    </div>\n" +
                            "                </li>\n" +
                            "            </ul>");
                    }
                } else {
                    $("#plans").append("抱歉，你的查找的数据不存在！嘿嘿嘿........请从新查找<a href=\"javascript:\" onclick=\"location.reload();\">点击返回</a>！");
                    tol = 1;
                }
            },
            error: function () {
                alert("出错了,抱歉！")
            }
        });
        $('#pagination').jqPaginator("option", {
            totalPages: tol
        });
    })
    //禁用文本框
    $("body").find("[name='dis']").attr("disabled", "disabled");

    //添加到收藏
    $("#btncollect").click(function () {
        var str = {
            accId: mctId,   //用户id
            mctId:merchantId
        };
        if (!(merchantId === undefined || mctId === undefined)) {
            $.ajax({
                type: "POST",
                url: "http://www.enet720.net:8080/zwkj/merchant/translate",
                contentType: 'application/json;charset=UTF-8',
                dataType: 'json',
                async: false,
                data: JSON.stringify(str),
                success: function (data) {
                    var data = data.data;
                    if (!data) {
                        alert("收藏失败")
                    } else {
                        alert("收藏成功")
                    }
                },
                error: function () {
                    alert("出错了,抱歉！")
                }
            });
        } else {
            alert("保存失败！没有用户名所以失败了。")
        }

    });
    //点击刷新重新加载
    $("#btncollect1").click(function () {
        location.reload();
    });
});


